package com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj;
import com.iflytek.uoamp.agent.common.util.ReplaceSpecialStr;
import com.iflytek.uoamp.agent.core.sender.monitorsender.monitorinterface.IItem;

import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

public class MonitorItemResult implements IItem{
	
	private Map<String,String> header = new HashMap<String, String>();
	private String componentType;
	private String host;
	private String serverDesc;
	private String returnCode;
	private String output;
	private String uptime;
	private String sendhost;
	private int hostObjectId;
	private int servicesObjectId;

	public void setHeader(Map<String,String> header){
		this.header = header;
	}
	
	public String getSendhost() {
		return sendhost;
	}

	public void setSendhost(String sendhost) {
		this.sendhost = sendhost;
	}

	public Map<String, String> getHeader() {
		return header;
	}

	public void putHeader(String key,String value){
		header.put(key, value);
	}
	
	public String getComponentType() {
		return componentType;
	}

	public void setComponentType(String componentType) {
		this.componentType = componentType;
	}
	
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getServerDesc() {
		return serverDesc;
	}

	public void setServerDesc(String serverDesc) {
		this.serverDesc = serverDesc;
	}

	public String getReturnCode() {
		return returnCode;
	}

	public void setReturnCode(String returnCode) {
		this.returnCode = returnCode;
	}

	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}

	public String getUptime() {
		if(uptime==null) return "";
		return uptime;
	}

	public void setUptime(String uptime) {
		this.uptime = uptime;
	}
	
	/**
	 * convert MonitorItemResult to byte for thrift.
	 * */
	public ByteBuffer toByte(){
		try {
			return ByteBuffer.wrap(toString().getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			Logger.getRootLogger().error("MonitorItemResult can't get bytes by utf-8",e);
			return ByteBuffer.allocate(0);
		}
	}

	private StringBuffer resultStrBuf = null;
	public String toString(){
//		if(resultStrBuf!=null) return resultStrBuf.toString();
		resultStrBuf = new StringBuffer();
		resultStrBuf.append("component_type"+(char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(componentType)+(char)31);
		resultStrBuf.append("host"+(char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(host)+(char)31);
		resultStrBuf.append("services_object_id" + (char) 30);
		resultStrBuf.append(servicesObjectId + "" + (char) 31);
		resultStrBuf.append("host_object_id" + (char) 30);
		resultStrBuf.append(hostObjectId + "" + (char) 31);
		resultStrBuf.append("server_desc"+(char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(serverDesc)+(char)31);
		resultStrBuf.append("return_code"+(char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(returnCode)+(char)31);
		resultStrBuf.append("output"+(char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(output)+(char)31);
		resultStrBuf.append("uptime"+(char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(uptime)+(char)31);
		resultStrBuf.append("sendhost"+(char)30);
		resultStrBuf.append(ReplaceSpecialStr.replaceSpecialStr(sendhost)+(char)31+"\n");
		return resultStrBuf.toString();
	}
	
	/**
	 * convert byte to MonitorItemResult for use.
	 * */
	public static MonitorItemResult getFromByte(ByteBuffer byteObj){
		MonitorItemResult result = new MonitorItemResult();
		String str = null;
		try {
			str = new String(byteObj.array(),"utf-8");
		} catch (UnsupportedEncodingException e1) {
			Logger.getRootLogger().error("MonitorItemResult bytes can't change to string by utf-8",e1);
			return result;
		}
		String[] kvs = str.split((char)31+"");
		for(String cur:kvs){
			String[] kv = cur.split(""+(char)30);
			try{
				if(kv[0].equals("component_type")){
					result.componentType = kv.length==2?kv[1]:null;
				}else if(kv[0].equals("host")){
					result.host = kv.length==2?kv[1]:null;
				}else if(kv[0].equals("server_desc")){
					result.serverDesc = kv.length==2?kv[1]:null;
				}else if(kv[0].equals("return_code")){
					result.returnCode = kv.length==2?kv[1]:null;
				}else if(kv[0].equals("output")){
					result.output = kv.length==2?kv[1]:null;
				}else if(kv[0].equals("uptime")){
					result.uptime = kv.length==2?kv[1]:null;
				}else if(kv[0].equals("sendhost")){
					result.sendhost = kv.length==2?kv[1]:null;
				} else if (kv[0].equals("services_object_id")) {
					result.servicesObjectId = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				} else if (kv[0].equals("host_object_id")) {
					result.hostObjectId = Integer.parseInt(kv.length == 2 ? kv[1] : null);
				}
			}catch(Exception e){
				Logger.getRootLogger().error("MonitorItemResult "+kv+" get value error!",e);
			}
		}
		return result;
	}

	@Override
	public String caseToString() {
		return "";
		//		return toString();
	}

	@Override
	public String getSendPath() {
		return "send";
	}

	@Override
	public String getSendListPath() {
		return "sendbatch";
	}
	
	public String getRltDes() {
		return "service: " + serverDesc + " returnCode: " + returnCode + " output: " + output;
	}

	public int getHostObjectId() {
		return hostObjectId;
	}

	public void setHostObjectId(int hostObjectId) {
		this.hostObjectId = hostObjectId;
	}

	public int getServicesObjectId() {
		return servicesObjectId;
	}

	public void setServicesObjectId(int servicesObjectId) {
		this.servicesObjectId = servicesObjectId;
	}

}
